package net.melon.product.inst.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.esj.basic.cache.AbstractCacheProxy;
import net.melon.product.inst.service.SpecificationsValueService;
import net.melon.product.pojo.ProductSpecificationsValue;

import org.springframework.stereotype.Service;

@Service("specificationsValueCacheProxy")
public class SpecificationsValueCacheProxy  extends AbstractCacheProxy<Map<Long,ProductSpecificationsValue>> implements SpecificationsValueService {

	public static String SPECIFICATION_VALUE_CACHE = "_specifications_value_cache_";
	
	@Resource
	private SpecificationsValueService specificationsValueService;
	
	
	public SpecificationsValueCacheProxy() {
		super(SPECIFICATION_VALUE_CACHE);
	}

	@Override
	public ProductSpecificationsValue getProductSpecificationsValue(Long id) {
		Map<Long,ProductSpecificationsValue> map = this.cache.get(SPECIFICATION_VALUE_CACHE);
		if(map==null){
			map = specificationsValueService.getList();
			this.cache.put(SPECIFICATION_VALUE_CACHE, map);
		}
		ProductSpecificationsValue value = map.get(id);
		return value;
	}
	
	@Override
	public ProductSpecificationsValue saveProductSpecificationsValue(ProductSpecificationsValue entity) {
		ProductSpecificationsValue value = specificationsValueService.saveProductSpecificationsValue(entity);
		cleanCache();
		return value;
	}

	@Override
	public ProductSpecificationsValue updateProductSpecificationsValue(ProductSpecificationsValue entity) {
		cleanCache();
		ProductSpecificationsValue value = specificationsValueService.updateProductSpecificationsValue(entity); 
		return value;
	}

	@Override
	public void deleteProductSpecificationsValue(Long id) {
		specificationsValueService.deleteProductSpecificationsValue(id);
		cleanCache();
	}
	
	public void deleteProductSpecificationsValue(Long id, boolean withProduct) {
		specificationsValueService.deleteProductSpecificationsValue(id,withProduct);
		cleanCache();
	}

	@Override
	public Map<Long,ProductSpecificationsValue> getList() {
		Map<Long,ProductSpecificationsValue> map = this.cache.get(SPECIFICATION_VALUE_CACHE);
		if(map==null){
			map = specificationsValueService.getList();
			this.cache.put(SPECIFICATION_VALUE_CACHE, map);
		}
		return map;
	}

	private void cleanCache(){
		this.cache.remove(SPECIFICATION_VALUE_CACHE) ;
	}

	public List<ProductSpecificationsValue> getSortList() {
		Map<Long,ProductSpecificationsValue> map = getList(); 
		List<ProductSpecificationsValue> list = new ArrayList<ProductSpecificationsValue>(map.values());
		Collections.sort(list, new Comparator<ProductSpecificationsValue>(){
			@Override
			public int compare(ProductSpecificationsValue o1,
					ProductSpecificationsValue o2) {
				if(o1.getId()>o2.getId()){
					return 1;
				}else if(o1.getId()<o2.getId()){
					return -1;
				}
				return 0;
			}
			
		});
		return list;
	}

}
